# -*- coding:utf-8 -*-
"""
Author：Administrator
Date:2021年04月16日
"""
from docx import Document
from docx.shared import Pt
from docx.oxml.ns import qn


document = Document("./长恨歌/长恨歌3.docx")
document.styles['Normal'].font.name = u'微软雅黑'
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'微软雅黑')
document.styles['Normal'].font.size = Pt(12)

def change_text(old_text, new_text):
	all_paragraphs = document.paragraphs
	for paragraph in all_paragraphs:
		for run in paragraph.runs:
			run_text = run.text.replace(old_text,new_text)
			run.text = run_text

		all_tables = document.tables
		for table in all_tables:
			for	row in table.rows:
				for cell in row.cells:
					cell_text = cell.text.replace(old_text,new_text)
					cell.text = cell_text

change_text('女' ,'W')
change_text('不' ,'ok')
document.save('./长恨歌/长恨歌替换.docx')